<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/27
 * Time: 17:08
 */

namespace app\service;

use app\models\Bank_bill;
use app\models\CouponModal;
use app\models\Express;
use app\models\Income;
use app\models\Inland;
use app\models\Invoice;
use app\models\Invoice_new;
use app\models\OrderModel;
use app\models\Pi;
use app\models\PrintModel;
use app\models\Product;
use app\models\Purchase;
use app\models\Repo;
use app\models\Staff;
use yii;

class OrderService
{
    public $model;
    public function __construct()
    {
        $this->model = new OrderModel();
    }

    /**支付订单号生成
     * @param $from  1paypal 2微信3支付宝
     * @param $type  1样品费2大货费3设计4摄像
     * @param $user_id
     */
    public function generateOrderNum($from, $type, $user_id)
    {
        $time     = substr(time(), -6);
        $order_id = $from . $type . $time . $user_id;
        return $order_id;
    }

    public function insertRecord($data)
    {
        return $this->model->insert($data);
    }

    public function orderDetail($in)
    {
        $data                = [];
        $model               = new Product();
        $data['productList'] = $model->get_invoice_product($in);

        $model          = new Invoice_new();
        $data['detail'] = $model->getDetailByInvoice($in);
        //获取in所有产品
        $model            = new Bank_bill();
        $data['bankList'] = $model->get_bank_info_by_in($in);
        //仓库费用添加
        $repo_model       = new Repo();
        $data['repoList'] = $repo_model->get_repo_info_by_invoice($in);
        //印刷费添加
        $print_model       = new PrintModel();
        $data['printList'] = $print_model->get_print_list_by_invoice($in);
        //内陆费
        $inland_model       = new Inland();
        $data['inlandList'] = $inland_model->get_inland_list_by_invoice((array) $in);

        $invoice_model = new Invoice_new();
        $info          = $invoice_model->get_invoice_info_by_invoice($in);

        //invoice 收入$添加
        $model              = new Income();
        $data['incomeList'] = $model->get_income_by_invoice($in);

        $express_model       = new Express();
        $data['expressList'] = $express_model->get_all_express_money($in);

        $data['expressType'] = $express_model->get_express_type();

        //读取采购单合同信息
        $purchase      = new Purchase();
        $purchase_list = $purchase->all(['invoice' => $in]);
        $arr           = [];
        foreach ($purchase_list as $v) {
            $tmp = json_decode($v['product_list'], true);
            foreach ($tmp as $v2) {
                array_push($arr, $v2);
            }
        }
        $data['purchaseList'] = $arr;
        return $data;
    }

    public function generateOrder($id)
    {
        //通过业务员invoice生成
        $model = new Invoice_new();
        do {
            $invoiceNum = $model->generateNum();
        } while ($model->one(['invoice' => $invoiceNum]));
        //更新invoice信息
        yii::$app->db->createCommand()->update('jingsourcing_invoice', ['invoice_id' => $invoiceNum], ['id' => $id])->execute();
        //生成订单
        $invoiceModal = new Invoice();
        $detail       = $invoiceModal->one(['id' => $id]);
        $productList  = json_decode($detail['product_list'], true);
        $total        = 0;
        foreach ($productList as $v) {
            $total += $v['subtotal'];
        }
        //添加服务费
        $service = json_decode($detail['service'], true);
        if ($service['show'] == 'true') {
            $total += $service['fee'];
        }
        //运费
        $ship = json_decode($detail['ship'], true);
        if ($ship['show'] == 'true') {
            $total += $ship['ship'];
        }

        //duty
        $duty = json_decode($detail['duty'], true);
        if ($duty['show'] == 'true') {
            $total += $duty['fee'];
        }
        //读取优惠券信息
        if ($detail['coupon_num']) {
            $couponModal = new CouponModal();
            $info        = $couponModal->one('*', ['number_no' => $detail['coupon_num']]);
            $total -= $info['detail']['free'];
        }
        //pi编号
        $piModal = new Pi();
        $piModal->
            $saleman   = '';
        $staff     = new Staff();
        $staffList = $staff->get_all_saleman();
        foreach ($staffList as $v) {
            if ($v['job_number'] == $detail['job_number']) {
                $saleman = $v['name'];
            }
        }
        $customer              = json_decode($detail['customer_info'], true);
        $data                  = [];
        $data['date']          = date('Y-m-d');
        $data['saleman']       = $saleman;
        $data['saleman_num']   = $detail['job_number'];
        $data['invoice']       = $invoiceNum;
        $data['customer_name'] = $customer['buyer'];
        $data['customer_num']  = $detail['number'];
        $data['order_pay']     = $total;
        $model->insert($data);
    }

    /**
     * 判断是否结算
     *@return bool
     */
    public function isJiesuan($where = [])
    {
        $model = new Invoice_new();
        $re    = $model->one($where);
        if ($re) {
            if ($re['is_jiesuan'] == 3) {
                return true;
            }
        }

        return false;

    }
}
